package com.apes.fico.engine.service;


import com.apes.fico.engine.model.VoucherTemplate;
import com.apes.fico.engine.repository.VoucherTemplateRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.Listener;
import com.apes.framework.plugin.outbox.OutboxSubscriber;
import com.apes.framework.plugin.outbox.OutboxSubscriberManager;
import com.apes.framework.plugin.soe.metadata.SceneRole;
import com.apes.framework.util.JpaUtil;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("voucherTemplateService")
public class VoucherTemplateService extends DomainService {

    @Autowired
    VoucherTemplateRepository voucherTemplateRepository;

    @Autowired
    private EntityManager entityManager;

    @Autowired
    private OutboxSubscriberManager outboxSubscriberManager;

    public VoucherTemplate create(SimpleRequest request) {
        VoucherTemplate voucherTemplate = request.getO(VoucherTemplate.class);
        return voucherTemplateRepository.saveAndFlush(voucherTemplate);
    }

    @Listener(topic = "outbox.subscriber.init")
    public List<OutboxSubscriber> getOutboxSubscribers(SimpleRequest request) {
        List<OutboxSubscriber> outboxSubscribers = new ArrayList<>();
        voucherTemplateRepository.findAll().stream().forEach(m -> {
            String description = m.getDescription();
            String classSimpleName = m.getBusinessType().getId();
            OutboxSubscriber outboxSubscriber = createOutboxSubscriber(description, classSimpleName, "S");
            if (outboxSubscriber == null) return;
            if (outboxSubscriber != null) outboxSubscribers.add(outboxSubscriber);
        });
        OutboxSubscriber outboxSubscriber = outboxSubscriberManager.createOutboxSubscriber(VoucherTemplate.class, "fin.template.add.subscriber", "新增VoucherTemplate触发事件订阅", "S", "fin.engine.addOutboxSubscriberByVoucherTemplate");
        outboxSubscribers.add(outboxSubscriber);
        return outboxSubscribers;
    }

    public void addOutboxSubscriberByVoucherTemplate(SimpleRequest request) {
        VoucherTemplate voucherTemplate = request.getO(VoucherTemplate.class);
        String classSimpleName = voucherTemplate.getBusinessType().getId();
        OutboxSubscriber outboxSubscriber = createOutboxSubscriber(voucherTemplate.getDescription(), classSimpleName, "S");
        if (outboxSubscriber == null) return;
        outboxSubscriberManager.addOutboxSubscriber(outboxSubscriber);
    }

    private OutboxSubscriber createOutboxSubscriber(String description, String classSimpleName, String eventType) {
        Class clazz = JpaUtil.getDomainClass(classSimpleName);
        if (clazz == null) return null;
        String outboxSubscriberId = "fin.engine." + classSimpleName;
        String outboxSubscriberName = "会计凭证引擎事件拦截："  + description;
        OutboxSubscriber outboxSubscriber = outboxSubscriberManager.createOutboxSubscriber(clazz, outboxSubscriberId, outboxSubscriberName, eventType, "fin.engine.subscribe");
        return outboxSubscriber;
    }
}
